define(['common/js/service/user-request-service-0.1',
        'common/js/service/boss-storage-service-0.1'],
    function () {
        controllerModule.controller("LoginController", ["$scope", "UserService", "cookieDataStorage", "dataStorage", "MaiDialog", function ($scope, UserService, cookieDataStorage, dataStorage, MaiDialog) {
        	console.log("LoginController");
        	$scope.errorMsg = "";
            $scope.userInfo = {};
            //记住用户名
            var userAccount = localStorage.getItem("userInfo.rememberAccount");
            if (userAccount) {
                $scope.userInfo.account = userAccount;
                $scope.userInfo.rememberAccount = true;
            };
            $scope.userLogin = function () {
                $scope.errorMsg = "";
                if (!$scope.userInfo.account) {
                    $scope.errorMsg = "请输入用户名";
                    return;
                }
                if (!$scope.userInfo.password) {
                    $scope.errorMsg = "请输入密码";
                    return;
                }

                UserService.login({
                    'account': encodeURI($scope.userInfo.account),
                    'password': $scope.userInfo.password
                }).then(function (data) {
                    if (data == null) {
                        $scope.errorMsg('系统异常，请稍后再试');
                    } else {
                        if (200 == data.dealcode) {
                            dataStorage.clearAll();
                            cookieDataStorage.clearItem("currentUser");
                            dataStorage.clearItem("menus");
                            delete $scope.userInfo.password;//浏览器前端不要记录用户密码
                            cookieDataStorage.saveItem("currentUser", $scope.userInfo);
                            window.location.href = rootPath + "/view/main/index";
                        } else {
                            $scope.errorMsg = data.dealmsg;
                        }
                    }

                    //是否记住用户名
                    if ($scope.userInfo.rememberAccount) {
                        localStorage.setItem("userInfo.rememberAccount", $scope.userInfo.account);
                    } else {
                        localStorage.removeItem("userInfo.rememberAccount");
                    }
                });

            };
            $scope.keyEnterLogin = function (ev) {
                if (ev.keyCode !== 13) return;
                $scope.userLogin();
            };
        }]);
        
        controllerModule.controller("UserInfoController", ["$scope", "$location", "UserService", "cookieDataStorage", "dataStorage", "MaiDialog", function ($scope, $location, UserService, cookieDataStorage, dataStorage, MaiDialog) {
            $scope.messageShow = false;
            $scope.checkMessageShow = function () {
                 $scope.messageShow = true;
            };
            $scope.checkMessageShow();
            $scope.userValidation = {
                oldpassword: {
                    required: "旧账户密码不能为空",
                    minlength: "旧密码长度不能小于6",
                    maxlength: "旧密码长度不能大于16",
                    pattern: "旧密码为6~16位数字或字母组成，区分大小写"
                },
                password: {
                    required: "新账户密码不能为空",
                    minlength: "新密码长度不能小于6",
                    maxlength: "新密码长度不能大于16",
                    pattern: "新密码为6~16位数字或字母组成，区分大小写"
                }, confirmPassword: {
                    required: "确认密码不能为空"
                }
            };

            $scope.changePwd = function () {
                $("#changePwd").modal("show");
            }
            $scope.save = function (params) {
                if (params.oldpassword == params.password) {
                    $scope.$broadcast('setFormMsg-UserForm', '新旧密码不能一样');
                    return false;
                }
                if (params.confirmPassword != params.password) {
                    $scope.$broadcast('setFormMsg-UserForm', '两次输入的密码不一致');
                    return false;
                }
                delete params.confirmPassword;
                UserService.changePwd({
                    'oldpassword': params.oldpassword,
                    'password': params.password
                }).then(function (data) {
                    if (data == null || data.dealcode != 200) {
                        MaiDialog.error(data == null ? "修改密码失败" : data.dealmsg);
                    } else {
                        MaiDialog.alert("密码修改成功");
                        $scope.$broadcast("clearForm-UserForm");
                        $("#changePwd").modal("hide");
                    }
                });
            }

            $scope.userInfo = cookieDataStorage.getItem("currentUser");
            $scope.signOut = function () {
            	sessionStorage.removeItem("alert");
                UserService.logout().then(function (data) {
                    //清除session
                    cookieDataStorage.clearItem("currentUser");
                    dataStorage.clearItem("menus");
                    if (data == null) {
                        MaiDialog.error('登出异常，请直接关闭浏览器');
                    } else {
                        if (200 == data.dealcode) {
                            //跳转至登录页
                            window.location.href = rootPath + "/view/main/login";
                        } else {
                            if (typeof data == 'string' && data.indexOf("ng-app") >= 0) {
                                window.location.reload();
                            } else {
                                MaiDialog.error('登出异常，请直接关闭浏览器');
                            }
                        }
                    }
                });

                //关闭售票副屏
                if ($.crosstab) {
                    $.crosstab.clearCrosstab();
                    $.crosstab.sendTerminateMessage();
                }
            };
        }]);
    });